package org.zpmis.common.base;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import org.zpmis.common.constant.UserConstants;
import org.zpmis.common.exception.BusinessException;
import org.zpmis.common.result.ResultCode;
import org.zpmis.common.utils.RedisUtil;
import org.zpmis.common.utils.StringUtil;
import org.zpmis.common.utils.json.GsonUtil;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

/**
 * 项目抽象controller
 */
@Slf4j
public abstract class BaseController {

    @Resource
    private RedisUtil redisUtil;

    @Autowired
    protected HttpSession session;

    protected HttpServletRequest getRequest(){
        HttpServletRequest request = ((ServletRequestAttributes) (RequestContextHolder.getRequestAttributes())).getRequest();
        return request;
    }

    /**
     * 从请求头中获取token
     *
     * @return
     */
    protected String getTokenFromHeader() {
        return getRequest().getHeader("Authorization");
    }

    /**
     * 获取当前登录小程序用户JSON
     * @return
     */
    protected AppletsLoginUser getAppletsLoginUser(){
        //AppletsLoginUser appletsLoginUser = (AppletsLoginUser)redisUtil.get(String.format(UserConstants.USER_TOKEN_REDIS_KEY, getTokenFromHeader()));
        String tokenFromHeader = getTokenFromHeader();
        if(StringUtil.isEmpty(tokenFromHeader)){
            throw new BusinessException(ResultCode.USER_NOT_LOGIN);
        }
        Object o = redisUtil.get(String.format(UserConstants.APPLETS_USER_TOKEN_REDIS_KEY, tokenFromHeader));
        if(o == null){
            throw new BusinessException(ResultCode.USER_NOT_LOGIN);
        }
        Object parse = JSON.parse(o.toString());
        AppletsLoginUser appletsLoginUser = JSONObject.parseObject(JSONObject.toJSONString(parse),AppletsLoginUser.class);
        if(!"1".equals(appletsLoginUser.getLoginType())){
            throw new BusinessException(ResultCode.USER_NOT_LOGIN);
        }
        return appletsLoginUser;
    }

    /**
     * 获取当前登录admin用户JSON
     * @return
     */
    protected AdminLoginUser getAdminLoginUser(){
        //AppletsLoginUser appletsLoginUser = (AppletsLoginUser)redisUtil.get(String.format(UserConstants.USER_TOKEN_REDIS_KEY, getTokenFromHeader()));
        String tokenFromHeader = getTokenFromHeader();
        if(StringUtil.isEmpty(tokenFromHeader)){
            throw new BusinessException(ResultCode.USER_NOT_LOGIN);
        }
        Object o = redisUtil.get(String.format(UserConstants.ADMIN_USER_TOKEN_REDIS_KEY, tokenFromHeader));

        if(o == null){
            throw new BusinessException(ResultCode.USER_NOT_LOGIN);
        }
        Object parse = JSON.parse(o.toString());
        AdminLoginUser adminLoginUser = JSONObject.parseObject(JSONObject.toJSONString(parse),AdminLoginUser.class);

        if(!"2".equals(adminLoginUser.getLoginType())){
            throw new BusinessException(ResultCode.USER_NOT_LOGIN);
        }
        return adminLoginUser;
    }

    /**
     * 获取请求的ip地址
     *
     * @return
     */
    protected final String getIpAddr() {
        String ip = getRequest().getHeader("x-forwarded-for");
        if (StringUtils.isEmpty(ip) || "unknown".equalsIgnoreCase(ip)) {
            ip = getRequest().getHeader("Proxy-Client-IP");
        }

        if (StringUtils.isEmpty(ip) || "unknown".equalsIgnoreCase(ip)) {
            ip = getRequest().getHeader("WL-Proxy-Client-IP");
        }

        if (StringUtils.isEmpty(ip) || "unknown".equalsIgnoreCase(ip)) {
            ip = getRequest().getRemoteAddr();
        }

        return ip;
    }

}
